package com.example.demo;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//返回类型为页面 RestController = Controller + ResponseBody
@Controller
@RequestMapping("/controller")
public class controller {
    //返回一个html页面
    @RequestMapping("/r1")
    public Object getHtml(){
        return "/index.html";
    }
    //将Html以数据的形式返回,此时若不加ResponseBody则程序会去寻找“<h1>helloSpring<h1>”这段路径
    //最终报路径不存在的错误
    @ResponseBody   //限制返回的类型为数据
    @RequestMapping("/r2")
    public String returnHtml(){
        return "<h1>helloSpring<h1>";   //这段为数据而不是页面，<h1>标签浏览器会自动解析
    }
    @RequestMapping("/r3")
    public String returnCss(){
        return "/test.css"; //已删除
    }
    //设置状态码
    @RequestMapping("/r4")
    @ResponseBody
    public String setStatus(HttpServletResponse response){
        response.setStatus(401);
        return "状态码设置成功";
    }
    //设置响应头的数据类型Content-Type
    @ResponseBody
    @RequestMapping(value = "/r5",produces = "application/json")
    public String setType(HttpServletResponse response){
        return "1123";
    }
}
